//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Reader.Components;

namespace CommunityServer.Reader.Controls
{
	/// <summary>
	/// Summary description for FolderListing.
	/// </summary>
	public class ViewPostDisplay : FeedReaderTemplatedWebControl
	{

		#region Private Members
		
		private Literal lTitle;
		private Literal lAuthor;
		private Literal lDescription;
		private Literal lPubDate;
		private Literal lCommentsUrl;
		private Literal lLink;

		#endregion

		#region Constructor(s)

		public ViewPostDisplay()
		{
			
		}

        protected override void Authorize()
        {
            //any one can view this control
        }


		#endregion

		#region Class Methods

		protected override void AttachChildControls()
		{
			
			lTitle				= (Literal) FindControl("lTitle");
			lAuthor				= (Literal) FindControl("lAuthor");
			lDescription		= (Literal) FindControl("lDescription");
			lPubDate			= (Literal) FindControl("lPubDate");
			lCommentsUrl		= (Literal) FindControl("lCommentsUrl");
			lLink				= (Literal) FindControl("lLink");
			
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);
			if(!Page.IsPostBack)
				DataBind();
		}


		public override void DataBind()
		{
			base.DataBind ();
			
			
			int feedPostId = 0;
			
			
			if (Page.Request.QueryString["FeedPostId"] != null)
			{
				feedPostId = Convert.ToInt32(Page.Request.QueryString["FeedPostId"]);
			}

			if (feedPostId != 0)
			{
				try
				{
				    FeedPost post = FeedReaderDataProvider.Instance().GetPostFullDetails(feedPostId);

					lTitle.Text			= post.Title;
					lAuthor.Text		= post.Author;
					lDescription.Text	= ((IContent)post).RenderedBody(PostTarget.Web);
					lPubDate.Text		= post.PubDate.ToString();


					if (post.Creator != null && post.Creator != "")
					{
						lPubDate.Text = lPubDate.Text + " by <a href=\"" + post.FeedLink + "\" target=\"_blank\">" + post.Creator + "</a>";	
					}

					if (post.CommentsUrl == null && post.CommentsUrl.Trim() == "")
					{
						lCommentsUrl.Visible = false;
					}
					else
					{
						if (post.CommentCount != int.MinValue)
						{
							lCommentsUrl.Text = "<a href=\"" + post.CommentsUrl + "\" target=\"_blank\">" + post.CommentCount + " comments</a>";
						}
						else
						{
							lCommentsUrl.Text = "<a href=\"" + post.CommentsUrl + "\" target=\"_blank\">View comments</a>";	
						}
						
					}
					
					lLink.Text = post.Link;



				}
				catch (Exception exc)
				{
					Console.Write(exc.Message);
				}

			}			
		}

		#endregion


	}
}
